<?php
	/**
	* 城市地区类
	* @author zhufu <zhufu159239@163.com>
	* @version 1.0
	* @date 2012-07-24 14:39:44
	*/
	class city extends core{

		private $table_city = 'nfbm_city_area'; //
		private $table_city_area = 'nfbm_city_area'; // 城市区域表

		private  $zx_city=array(110000,120000,310000,500000,810000,910000);



		/**
		* 构造函数
		*
		* @return void
		*/
		public function __construct() {

		}

		/**
		* 析构函数
		*
		* @return void
		*/
		public function __destruct() {

		}


		/********  内部方法  *******************/




		/********  外部方法  *******************/

		/**
		* 返回直辖市id
		*
		*/
		public function zx_city(){
			return $this->zx_city;
		}


		/**
		* 根据id,得到城市信息
		* @author <zhufu159239@163.com> 2012-09-17 12:02:53
		*
		* @param int $id    记录id
		* @return array
		*/
		public function out_info($id){
			//检查参数
			if(!is_numeric($id)){
				return false;
			}
			$sql = "SELECT * FROM {$this->table_city} WHERE id={$id}";
			return $this->memcache_findone($sql);
		}

		/**
		* 得到城市列表
		* @author <zhufu159239@163.com> 2012-07-25 16:29:43
		*
		* @param int $pid   上级id，如果为默认（0），得到一级城市
		* @return array
		*/
		public function out_list($pid=0){
			//检查参数
			$pid = is_numeric($pid) ? $pid : 0;
			//操作数据库
			$sql = "SELECT * FROM {$this->table_city} WHERE pid={$pid} AND `state`=0";
			return $this->memcache_find($sql);
		}

		/**
		* 城市列表
		* @author zhufu 2013-07-26
		*
		* @param int $pid 上级ID
		*/
		public function out_list_2($pid=0,$child=0){
			$sql = "SELECT * FROM {$this->table_city_area} WHERE 1";
			if($pid > 0){
				$tmp = substr($pid, 0, 2);
				$tmp2 = substr($pid, 0, 4);
				if($tmp . '0000' == $pid){
					//是一级城市
					if($child){
						$sql .= " AND id LIKE '{$tmp}____' AND id NOT LIKE '{$tmp}0000'";
					}
					else{
						$sql .= " AND id LIKE '{$tmp}__00' AND id NOT LIKE '{$tmp}0000'";
					}
				}
				else if($tmp2 . '00' == $pid){
					//是二级城市
					$sql .= " AND id LIKE '{$tmp}__' AND id NOT LIKE '{$tmp}00'";
				}
			}
			else{
				if($child == 1){
					//要一级子级
					$sql .= " AND id LIKE '____00'";
				}
				else if($child == 0){
					//不要子级
					$sql .= " AND id LIKE '__0000'";
				}
			}
			$sql .= " ORDER BY id ASC";
			return $this->memcache_find($sql);
		}

		/**
		* 根据城市ID获得城市信息
		* @author limin  2013-4-28 AM 11:41:00
		* @param  mixed  $id  城市ID
		* @param  mixed  $level  区域级别
		*/
		public function  _city_list($id,$level=0,$sort="`spelling` ASC"){
			if(!is_numeric($id)||!is_numeric($level)){
				return false;
			}
			switch($level){
				case 1:
					if($id){
						$str = substr($id,0,2);
						$sql="SELECT * FROM {$this->table_city_area} WHERE id = '{$str}0000'";
					}else{
						$sql="SELECT * FROM {$this->table_city_area} WHERE id LIKE '__0000'";
					}
					$province_dir="province/";
					break;
				case 2:
					$str = substr($id,0,2);
					$path = $str.'0000';
					$sql = "SELECT * FROM {$this->table_city_area} WHERE state=0 AND id LIKE '{$str}__00' AND id<>'{$str}0000'";
					foreach($this->zx_city as $k=>$v){
						if($id==$v){
							$sql = "SELECT * FROM {$this->table_city_area} WHERE id LIKE '{$str}__00' AND id<>{$str}0100 AND id<>{$str}0200";
							break;
						}
					}
					$city_dir="province/{$path}/";
					break;
				case 3:
					$str = substr($id,0,4);
					$path1 = substr($id,0,2).'0000';
					$path2 = $str.'00';
					$sql = "SELECT * FROM {$this->table_city_area} WHERE id LIKE '{$str}__' AND id <> '{$str}00'";
					foreach($this->zx_city as $k=>$v){
						if($id==$v){
							$str = substr($id,0,2);
							$sql = "SELECT * FROM {$this->table_city_area} WHERE id LIKE '{$str}%' AND id<>'{$str}0100' AND id<>'{$str}0200'";
							//如果城市为香港、澳门，区域为城市本身，否则则为城市下的区域
							if($id!=810000&&$id!=910000){
								$sql.=" AND id<>'{$str}0000'";
							}
							break;
						}

					}
					$area_dir="province/{$path1}/{$path2}";
					break;
			}
			$sql .= " ORDER BY {$sort}";//默认`spelling` ASC
			$list = $this->memcache_find($sql);

			$tmp_array=array();

			if($list){
				foreach ($list as $v){
					$tmp_array[$v['id']]=$v;
				}
			}else{
				return false;
			}

			//return $list;
			if($province_dir){
				F($province_dir.'one',$tmp_array);

				$file=F($province_dir.'one');
				$list=$file?$file:$tmp_array;
				return $tmp_array ;
			}
			if($city_dir){
				F($city_dir.'two',$tmp_array);
				$file=F($city_dir.'two');
				$list=$file?$file:$tmp_array;
				return $tmp_array ;
			}
			if($area_dir){
				F($area_dir,$tmp_array);
				$file=F($area_dir);
				$list=$file?$file:$tmp_array;
				return $tmp_array ;
			}

		}


		/**
		* 根据城市id得到城市信息
		* @author limin  2013-5-3 PM 16:37:00
		* @param int $id    记录id
		* @return array
		*/
		public function _city_info($id){
			//检查参数
			if(!is_numeric($id)){
				return false;
			}

			$province = F('province/one');
			if($province){
				$data=$this->_city_list(0,1);
				F("province/one",$data);
				$res=$data[$id];
				if($res) return $res;
			}
			if(!$res){ //二级查找
				$key = in_array($id,$this->zx_city)?substr($id,0,2).'0000':substr($id,0,4).'00';
				$data=$this->_city_list($id,2);
				F('province/'.$key.'/two',$data);
				$res=$data[$id];
				if($res) return $res;
			}
			if(!$res){ //三级查找
				$key = in_array($id,$this->zx_city)?substr($id,0,2).'0000':substr($id,0,4).'00';
				$area = substr($id,0,4).'00';
				$data=$this->_city_list($id,3);
				F("province/{$key}/{$area}",$data);
				$res=$data[$id];
				if($res) return $res;
			}
			return false;
		}


		/**
		* 获得城市区域信息
		* @author limin  2013-5-4  PM  13:31:00
		* @param  mixed  $id  城市ID
		* @param  mixed  $level  区域级别
		*/
		public function _area_list($id){
			if(!is_numeric($id)){
				return false;
			}
			$str = substr($id,0,4);
			$sql = "SELECT * FROM {$this->table_city_area} WHERE id LIKE '{$str}__' AND id<>'{$str}00' AND city_name <> '市辖区' and city_name <> '县'";
			foreach($this->zx_city as $k=>$v){
				if($id==$v){
					$str = substr($id,0,2);
					$sql = "SELECT * FROM {$this->table_city_area} WHERE id LIKE '{$str}%' AND id NOT IN ({$str}0000,{$str}0100,{$str}0200)";
					break;
				}
			}
			$list = $this->memcache_find($sql);
			return $list ;
		}




		/**
		* 根据城市名得到城市信息
		* @author limin 2013-5-6 PM 16:38:00
		* @param string  $name  城市名称
		*/
		public function _city_by_name($name){
			if(!$name){
				return false;
			}
			$sql="SELECT * FROM {$this->table_city_area} WHERE city_name LIKE '{$name}%'";
			return $this->memcache_findone($sql);
		}


		/**
		* 根据城市名得到城市信息
		* @author limin 2013-5-6 PM 16:38:00
		* @param string  $spell  拼音缩写
		*/
		public function _city_by_spell($spell){
			if(!$spell){
				return false;
			}
			$sql="SELECT * FROM {$this->table_city_area} WHERE spelling ='{$spell}'";
			return $this->memcache_findone($sql);
		}

		/**
		* 根据拼音首字母获得城市信息
		* @author limin 2013-5-6   AM 10:30:00
		* @param  string  $letter   城市首字母
		*/
		public function _get_cities_by_spell($letter){
			if(!$letter){
				return false;
			}
			//组织sql语句
			$sql="select * from my_city_area where spelling like '{$letter}%' and ((id like '____00' and id not like '__0000'
			) or main>0) order by city_name";
			return $this->memcache_find($sql);
		}

		/**
		* 得到开通的城市
		* @author zhufu 2013-09-10
		*
		*/
		public function out_list_main($main=0){
			$sql = "SELECT * FROM {$this->table_city_area} WHERE ";
			if($main){
				$sql .= "main>1";
			}
			else{
				$sql .= "main>0";
			}
			return $this->memcache_find($sql);
		}

		/**
		* 根据ID，得到已经开通的城市
		* @author zhufu 2013-03-23
		* @param int $id 获取的实际城市的ID
		*/
		public function get_opend_city($id=false){
			if(!is_numeric($id)){
				//如果没有传入ID，则得到所有开通的城市
				$sql = "SELECT id as code,city_name as name,SUBSTR(spelling,1,1) as letter FROM {$this->table_city} WHERE main>0 ORDER BY letter ASC";
				return $this->memcache_find($sql);
			}
			//得到对应的二级ID
			$id2 = substr($id, 0, 4) . str_repeat('0', 2);
			//一级id
			$area = substr($id, 0, 2);

			$sql = "select id,city_name from {$this->table_city} where main=1 and id={$id2}";
			$row = $this->memcache_findone($sql);
			if($row){
				return $row;
			}
			//本身是二级，直接获取省份默认城市
			$sql = "select id,city_name from {$this->table_city} where main=2 and area={$area}";
			$row = $this->memcache_findone($sql);
			return $row;
		}

		/**
		* 根据id,修改城市排序
		* @author <zhufu159239@163.com> 2012-08-16 16:33:49
		*
		* @param int $id        要修改的记录id
		* @param array $data    要修改的数据
		*/
		public function _city_sort_edit($id, $data){
			//检查参数
			if(!$data || !is_numeric($id)){
				return false;
			}
			$where = "id={$id}";
			if(!$this->set_db($this->table_city, $data, false, $where)){
				return false;
			}
			//清除缓存
			$filename = 'cat/'.$this->table_admin_product_cat;
			F($filename, null);
			return true;
		}
	}
?>
